package com.cl.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

 
	/**
	 * json工具类
	 * 
	 */
	public class JsonUtil {
		 
		 private static Gson gson = new Gson();
		 
		/**
		 * Json字符串生成对象
		 * @param json
		 * @param type
		 * @return
		 */
		public static <T> Object fromJson(String json ,TypeToken<T> type) {
				return gson.fromJson(json,type.getType());
			}
			
			
			
		
		
		/**
		 * Map生成json字符串
		 * 
		 * @param maps
		 * @return String
		 */
		public static <T> String mapToJson(Map<String, T> map) {
			String jsonStr = gson.toJson(map, new TypeToken<Map<String, T>>() {
			}.getType());
			return jsonStr;
		}
		
		/**
		 * List生成json字符串
		 * 
		 * @param List
		 * @return String
		 */
		public static <T> String listToJson(List<T> event) {
			String jsonStr = gson.toJson(event, new TypeToken<List<T>>() {
			}.getType());
			return jsonStr;
		}
		
		
		
		/**
		 * 普通对象生成 json字符串
		 * @param obj
		 * @return
		 */
		public static String genJsonString(Object obj){
			try {
				return gson.toJson(obj);
			} catch (Exception ex) {
				return "";
			}
		}
		
		/**
		 * json字符串 转换成 对象
		 * @param str
		 * @param clazz
		 * @return
		 */
		public static <T> T parseBean(String str, Class<T> clazz) {
			try {
				return new Gson().fromJson(str, clazz);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		

		/**
		 * 解析string
		 * 
		 * @param str
		 * @return
		 */
		public static JsonObject parseString(String str) {
			try {
				Gson gson = new Gson();
				return gson.fromJson(str, JsonObject.class);
			} catch (Exception ex) {
				return new JsonObject();
			}
		}

		/**
		 * 合并json
		 * @param objs
		 * @return
		 */
		public static JsonObject merge(JsonObject... objs) {
			int size = objs.length;
			if (size <= 0) {
				return null;
			}
			JsonObject obj1 = objs[0];

			for (int i = 0; i < size; i++) {
				JsonObject obj = objs[i];
				Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
				for (Iterator<Map.Entry<String, JsonElement>> iter = entrySet
						.iterator(); iter.hasNext();) {
					Map.Entry<String, JsonElement> entry = iter.next();
					String key = entry.getKey();
					if (!obj1.has(key)) {
						JsonElement value = entry.getValue();
						obj1.add(key, value);
					}
				}
			}
			return obj1;
		}

		/**
		 * 获取json对象
		 * 
		 * @param json
		 * @param member
		 * @return
		 */
		public static JsonObject getJsonObject(JsonObject json, String member) {
			try {
				JsonElement em = json.get(member);
				return em.getAsJsonObject();
			} catch (Exception ex) {
				return new JsonObject();
			}
		}

		/**
		 * 获取json数组对象
		 * 
		 * @param json
		 * @param member
		 * @return
		 */
		public static JsonArray getJsonArray(JsonObject json, String member) {
			try {
				JsonElement em = json.get(member);

				return em.getAsJsonArray();
			} catch (Exception ex) {
				return new JsonArray();
			}
		}

		/**
		 * 获取string对象
		 * 
		 * @param json
		 * @param member
		 * @return
		 */
		public static String getJsonString(JsonObject json, String member) {
			try {
				JsonElement em = json.get(member);

				if (em.isJsonArray()) {
					return em.toString();
				}
				return em.getAsString();
			} catch (Exception ex) {
				return "";
			}
		}

		/**
		 * 获取int对象
		 * 
		 * @param json
		 * @param member
		 * @return
		 */
		public static int getJsonInt(JsonObject json, String member) {
			try {
				JsonElement em = json.get(member);

				return em.getAsInt();
			} catch (Exception ex) {
				return 0;
			}
		}

		/**
		 * 获取boolean值
		 * 
		 * @param json
		 * @param member
		 * @return
		 */
		public static boolean getJsonBoolean(JsonObject json, String member) {
			try {
				JsonElement em = json.get(member);

				return em.getAsBoolean();
			} catch (Exception ex) {
				return false;
			}
		}
		
		 public static Gson getGson() {
				return gson;
			}

		 public static void setGson(Gson gson) {
				JsonUtil.gson = gson;
			}

	}

 
